package com.etoak.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.json.JSONUtil;
import com.etoak.common.VO.Email;
import com.etoak.config.EmailConfig;
import com.etoak.entity.User;
import com.etoak.mapper.UserMapper;
import com.etoak.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author et2406
 * @since 2024-11-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    RabbitTemplate rabbitTemplate;

    @Transactional(rollbackFor = Exception.class)
    // 1. 使用的前提是开启了事务扫描 <tx:annotation-driven>
    @Override
    public void registry(User user) {
        user.setCreateTime(DateUtil.now());
        user.setPassword(MD5.create().digestHex("123456"));

        this.save(user);

        Email email = new Email(user.getEmail(),"激活邮件",
                "请点击链接进行激活http://localhost:8080/user/active"+user.getUsername());

        String message = JSONUtil.toJsonStr(email);

        rabbitTemplate.convertAndSend(EmailConfig.EXCHANGE,EmailConfig.KEY,message);
    }
}
